#include "stdafx.h"
#include <Strsafe.h>
#include <dbt.h>
#include "PebDev.h"
#include "resource.h"

#define MAX_LOADSTRING	100

HINSTANCE	inst;								// current instance
HWND		window;

static TCHAR	title[MAX_LOADSTRING];			// The title bar text
static TCHAR	wndClass[MAX_LOADSTRING];		// the main window class name

static INT_PTR	CALLBACK	about(HWND,UINT,WPARAM,LPARAM);
static BOOL		WINAPI		initInstance(HINSTANCE,int);
static ATOM		WINAPI		myRegisterClass(HINSTANCE);
static LRESULT	CALLBACK	wndProc(HWND,UINT,WPARAM,LPARAM);

static INT_PTR CALLBACK about(HWND dlg,UINT msg,WPARAM wparam,LPARAM lparam){
	UNREFERENCED_PARAMETER(lparam);
	switch (msg){
	case WM_INITDIALOG:
		return(INT_PTR)TRUE;
	case WM_COMMAND:
		if (LOWORD(wparam)==IDOK || LOWORD(wparam)==IDCANCEL){
			EndDialog(dlg,LOWORD(wparam));
			return(INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
static BOOL WINAPI initInstance(HINSTANCE instance,int show){
	HWND w;
	inst=instance;				// Store instance handle in global
	w=CreateWindow(wndClass,title,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,
		CW_USEDEFAULT,0,NULL,NULL,instance,NULL);
	if(!w)
		return FALSE;
	ShowWindow(w,show);
	UpdateWindow(w);
	return TRUE;
}
static ATOM WINAPI myRegisterClass(HINSTANCE instance){
	WNDCLASSEX w;
	w.cbSize		=sizeof(WNDCLASSEX);
	w.style			=CS_HREDRAW | CS_VREDRAW;
	w.lpfnWndProc	=wndProc;
	w.cbClsExtra	=0;
	w.cbWndExtra	=0;
	w.hInstance		=instance;
	w.hIcon			=LoadIcon(instance,MAKEINTRESOURCE(IDI_PEBBLE));
	w.hCursor		=LoadCursor(NULL,IDC_ARROW);
	w.hbrBackground	=(HBRUSH)(COLOR_WINDOW+1);
	w.lpszMenuName	=MAKEINTRESOURCE(IDC_PEBBLE);
	w.lpszClassName	=wndClass;
	w.hIconSm		=LoadIcon(instance,MAKEINTRESOURCE(IDI_SMALL));
	return RegisterClassEx(&w);
}
static LRESULT wmCommand(HWND hwnd,WPARAM wparam,LPARAM lparam){
	int id= LOWORD(wparam);
	int ev= HIWORD(wparam);
	switch (id){
	case IDM_ABOUT:
		DialogBox(inst,MAKEINTRESOURCE(IDD_ABOUTBOX),hwnd,about);
		break;
	case IDM_EXIT:
		DestroyWindow(hwnd);
		break;
	default:
		return DefWindowProc(hwnd,WM_COMMAND,wparam,lparam);
	}
	return 0;
}
static LRESULT wmCreate(HWND hwnd,WPARAM wparam,LPARAM lparam){
	window=hwnd;
	PebDev::openAll();
	return DefWindowProc(hwnd,WM_CREATE,wparam,lparam);
}
static LRESULT wmDestroy(void){
	PebDev::closeAll();
	PostQuitMessage(0);
	return 0;
}
static LRESULT wmDeviceChange(HWND hwnd,WPARAM wparam){
	switch(wparam){
	case DBT_DEVICEREMOVECOMPLETE:
		PebDev::closeAll();
		break;
	case DBT_DEVNODES_CHANGED:
		PebDev::closeAll();
	case DBT_DEVICEARRIVAL:
		PebDev::openAll();
		break;
	default:
		return 1;
	}
	//ShowWindow(hkid,device==INVALID_HANDLE_VALUE?SW_HIDE:SW_SHOW);
	return 0;
}
static void wmPaint(HWND hwnd){
	PAINTSTRUCT ps;
	HDC hdc;
	hdc=BeginPaint(hwnd,&ps);
	EndPaint(hwnd,&ps);
}
static LRESULT CALLBACK wndProc(HWND hwnd,UINT message,WPARAM wparam,
	LPARAM lparam){
	switch(message){
	case WM_COMMAND:
		return wmCommand(hwnd,wparam,lparam);
	case WM_CREATE:
		return wmCreate(hwnd,wparam,lparam);
	case WM_DEVICECHANGE:
		return wmDeviceChange(hwnd,wparam);
	case WM_PAINT:
		wmPaint(hwnd);
		break;
	case WM_DESTROY:
		return wmDestroy();
	default:
		return DefWindowProc(hwnd,message,wparam,lparam);
	}
	return 0;
}

//-----------------------------------------------------------------------------

int APIENTRY _tWinMain(HINSTANCE instance,HINSTANCE prevInst,LPTSTR cmdLine,
	int show){
	UNREFERENCED_PARAMETER(prevInst);
	UNREFERENCED_PARAMETER(cmdLine);
	LoadString(instance,IDS_APP_TITLE,title,MAX_LOADSTRING);
	LoadString(instance,IDC_PEBBLE,wndClass,MAX_LOADSTRING);
	myRegisterClass(instance);
	if(!initInstance(instance,show))
		return FALSE;

	MSG msg;
	HACCEL accelTable=LoadAccelerators(instance,MAKEINTRESOURCE(IDC_PEBBLE));
	while (GetMessage(&msg,NULL,0,0)){
		if (!TranslateAccelerator(msg.hwnd,accelTable,&msg)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	PebDev::cleanup();
	return (int)msg.wParam;
}